<template>
  <div class="setting-box">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button
            type="primary"
            size="small"
            class="btn"
            @click="add"
          >+新增角色</el-button>
          <el-table :data="list" border>
            <el-table-column label="序号" width="50px">
              <template v-slot="{ $index }">
                {{ (page.page - 1) * page.pagesize + $index + 1 }}
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name" sortable />
            <el-table-column
              label="描述"
              prop="description"
              sortable
              width="500px"
            />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <el-button
                  type="text"
                  @click="setClick(row.id)"
                >分配权限</el-button>
                <el-button type="text" @click="edit(row)">修改</el-button>
                <el-button type="text" @click="del(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页器 -->
          <div class="page">
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 5, 100, 200, 300, 400]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="企业详情" lazy>
          <!-- lazy:延迟渲染即我不点这个栏就不调接口点才调 -->
          <!-- 使用组件 -->
          <Info />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 新增弹框 -->
    <Add ref="add" @getData="getData" />
    <!-- 权限分配弹框 -->
    <setPermission ref="setPermission" />
  </div>
</template>

<script>
import Add from './components/Add.vue'
import Info from './components/info.vue'
import { sysRole, sysRoleDel } from '@/api/setting'
import jsCookie from 'js-cookie'
import setPermission from './components/setPermission.vue'
export default {
  components: {
    Add,
    Info,
    setPermission
  },
  data() {
    return {
      list: [{}],
      page: {
        page: 1,
        pagesize: +jsCookie.get('setting-size') || 1,
        total: 100
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 页容量
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.page.pagesize = val
      jsCookie.set('setting-size', val)
      // 调用接口
      this.getData()
    },
    // 页码
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.page.page = val
      // 调用接口
      this.getData()
    },
    // 获取角色列表数据
    async getData() {
      const res = await sysRole(this.page)
      // 将数据保存到list中
      this.list = res.data.rows
      // 将total保存
      this.page.total = res.data.total
    },
    // 新增按钮打开弹框
    add() {
      // 打开弹框组件
      this.$refs.add.isShow = true
      // this.$refs.add即获取到该组件的vue实例对象
      this.$refs.add.mode = 'add'
    },
    // 点击删除，删除角色
    del(id) {
      // 给一个确认框
      this.$confirm('您确定要删除该角色吗', '温馨提示')
        .then(async() => {
          // 确定
          // 调用接口删除数据
          await sysRoleDel(id)
          this.$message.success('删除成功')
          // 判断该页数据是不是只有一条
          if (this.page.page > 1 && this.list.length <= 1) {
            this.page.page--
          }
          // 刷新数据
          this.getData()
        })
        .catch(() => {})
    },
    // 修改
    edit(row) {
      // 让弹框时出现
      this.$refs.add.isShow = true
      // 点击编辑按钮让弹框的模式为编辑
      this.$refs.add.mode = 'edit'
      // 数据回显到弹框中
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
    },
    // 权限分配
    setClick(id) {
      this.$refs.setPermission.isShow = true
      // 点击调出接口数据
      this.$refs.setPermission.getPermmisionData(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.setting-box {
  padding: 15px;
  .btn {
    margin: 10px 0;
  }
  .page {
    text-align: right;
    padding-top: 15px;
  }
}
</style>
